﻿//
// Created by Administrator on 2022/6/16.
//

#include "MyApp.h"
#include "MyDialog.h"
#include "wx/wx.h"
#include "wx/confbase.h"
#include "wx/stdpaths.h"
#include "wx/sysopt.h"
#include <wx/fs_zip.h> // ZIP filesystem support
#include <wx/tipdlg.h>
#include <wx/cshelp.h>
#include <wx/image.h>

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/locale.hpp>
#include <wx/snglinst.h>
#include "utils.h"
#include <cpr/cpr.h>

#define key_touch_server_ip "touch-server-ip"
#define key_touch_server_port "touch-server-port"
#define key_backup "backup"
#define key_delay "delay"
#define key_server_status_delay "server-status-delay"
#define key_message_delay "message-delay"

IMPLEMENT_APP ( MyApp )

string touch_server_ip;
int  touch_server_port;
bool backup;
int delay;
bool loopIt;
int server_status_delay;
int message_delay;

bool MyApp::OnInit() {

    auto* m_checker = new wxSingleInstanceChecker();
    if (m_checker->IsAnotherRunning()) {
        delete m_checker;
        m_checker = nullptr;
        return false;
    }

    if( argc!=2 ){
        wxString tmp;
        tmp.sprintf( "USAGE: %s config file path", argv[0] );
        GuiErrorMsg( tmp );
        exit(-1);
    }

    //parse the config file
    if ( !wxFile::Exists( argv[1] )) {
        GuiErrorMsg( wxString("config file not exists." ) );
        exit(-1);
    }
    boost::property_tree::ptree root_node, tag_system;
    boost::property_tree::ini_parser::read_ini( argv[1].ToStdString(),
                                               root_node);
    tag_system = root_node.get_child("main");

    if(tag_system.count( key_touch_server_ip) != 1) {
        GuiCmdParamErrorMsg( key_touch_server_ip );
        exit(-1);
    }
    touch_server_ip = tag_system.get<string>( key_touch_server_ip );
    std::cout << ( key_touch_server_ip )<<": "
              << touch_server_ip << std::endl;

    if(tag_system.count(key_touch_server_port ) != 1) {
        GuiCmdParamErrorMsg( key_touch_server_port );
        exit(-1);
    }
    touch_server_port = tag_system.get<int>( key_touch_server_port );
    std::cout << key_touch_server_port
            <<": " << touch_server_port << std::endl;

    if(tag_system.count(key_delay) != 1) {
        GuiCmdParamErrorMsg( key_delay );
        exit(-1);
    }

    delay = tag_system.get<int>( key_delay );
    std::cout << key_delay
        <<": " << delay << std::endl;

    if(tag_system.count(key_backup) != 1) {
        GuiCmdParamErrorMsg( key_backup );
        exit(-1);
    }

    backup = tag_system.get<bool>( key_backup );
    std::cout << key_backup
        <<": " << backup << std::endl;

    if(tag_system.count(key_server_status_delay) != 1) {
        GuiCmdParamErrorMsg( key_server_status_delay );
        exit(-1);
    }

    server_status_delay = tag_system.get<int>( key_server_status_delay );
    std::cout << key_server_status_delay
              <<": " << server_status_delay << std::endl;

    if(tag_system.count(key_message_delay) != 1) {
        GuiCmdParamErrorMsg( key_message_delay );
        exit(-1);
    }

    message_delay = tag_system.get<int>( key_message_delay );
    std::cout << key_message_delay
              <<": " << message_delay << std::endl;

    WSADATA WSAData;
    if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0){
        printf("初始化失败!");
        exit( -1 );
    }    //初始化

    loopIt = true;

    auto *dialog = new MyDialog();
    dialog->Show();
    return true;
}

int MyApp::OnExit() {

    return 0;
}